package com.sure.supermarketserver.stock.supplier;

import com.sure.supermarketserver.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/stock/supplier")
public class SupplierAPI {

    @Resource
    private SupplierService supplierService;

    @GetMapping("")
    public Result supplierList(SupplierDto dto) {
        Map<String, Object> page = supplierService.findSupplierList(dto);
        return Result.success(page);
    }

    @PostMapping("")
    public Result supplierAdd(@RequestBody SupplierDto dto) {
        try {
            supplierService.addSupplier(dto);
        }catch (Exception e){
            return Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage());
        }
        return Result.success();
    }

    @DeleteMapping("{id}")
    public Result supplierDel(@PathVariable String id) {
        supplierService.delSupplier(id);
        return Result.success();
    }

    @PutMapping("")
    public Result supplierUpd(@RequestBody SupplierDto dto) {
        supplierService.updSupplier(dto);
        return Result.success();
    }
}
